/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';

/// Enum defining the position of the extra region.
enum ExtraRegionPosition { top, bottom }

/// 区域比例,值为第一个区域的占用比例
class PresetSplitRatio {
  ///布局比例1 ：1,值为第一个区域的占用比例
  static const double layout_1V1 = 0.5;

  ///布局比例2 ：3,值为第一个区域的占用比例
  static const double layout_2V3 = 2.0 / 5.0;

  ///布局比例3 ：2,值为第一个区域的占用比例
  static const double layout_3V2 = 3.0 / 5.0;
}

/// Configuration options for the layout in the 'folded' display state.
class FoldedRegionLayoutOptions {
  /// The ratio for splitting the available space when arranged vertically.
  /// Defines the proportion of space allocated to the primary region.
  /// Values range from 0.0 to 1.0. Defaults to `PresetSplitRatio.layout_1V1`.
  double verticalSplitRatio;

  FoldedRegionLayoutOptions({
    this.verticalSplitRatio = PresetSplitRatio.layout_1V1,
  });
}

/// Configuration options for the layout in the 'expanded' display state.
class ExpandedRegionLayoutOptions {
  /// The ratio for splitting the available space when arranged vertically.
  /// Defines the proportion of space allocated to the primary region.
  /// Values range from 0.0 to 1.0. Defaults to `PresetSplitRatio.layout_1V1`.
  double verticalSplitRatio;

  /// The ratio for splitting the available space when arranged horizontally.
  /// Defines the proportion of space allocated to the primary region.
  /// Values range from 0.0 to 1.0. Defaults to `PresetSplitRatio.layout_3V2`.
  double horizontalSplitRatio;

  /// Determines if the extra region's layout axis is perpendicular to the
  /// primary/secondary split axis. If true, and P/S split horizontally,
  /// the extra region is added vertically (top/bottom). If false, it's added
  /// along the same axis.
  /// Defaults to `false`.
  bool isExtraRegionPerpendicular;

  /// Specifies the position of the extra region relative to the primary/secondary
  /// regions when `isExtraRegionPerpendicular` is true.
  /// Defaults to `ExtraRegionPosition.bottom`.
  ExtraRegionPosition extraRegionPosition;

  ExpandedRegionLayoutOptions(
      {this.verticalSplitRatio = PresetSplitRatio.layout_1V1,
      this.horizontalSplitRatio = PresetSplitRatio.layout_3V2,
      this.isExtraRegionPerpendicular = false,
      this.extraRegionPosition = ExtraRegionPosition.bottom});
}

/// Configuration options applied when the widget is hovered.
/// These can override settings from the current `DisplayState` options.
class HoverModeRegionLayoutOptions {
  /// Optional override for the vertical split ratio during hover.
  ExtraRegionPosition extraRegionPosition;

  /// Optional override for the horizontal split ratio during hover.
  double horizontalSplitRatio;

  /// Explicitly show or hide the extra region during hover.
  /// If null, the visibility depends on the base layout options.
  bool showExtraRegion;

  HoverModeRegionLayoutOptions({
    this.showExtraRegion = false,
    this.horizontalSplitRatio = PresetSplitRatio.layout_3V2,
    this.extraRegionPosition = ExtraRegionPosition.top,
  });
}

/// Represents the hover status, primarily indicating if hover mode is active.
class HoverModeStatus {
  /// True if the widget is currently being hovered over.
  final bool isHoverMode;

  // Note: appRotation and windowStatusType from HarmonyOS are omitted
  // as they are complex, platform-dependent, and not part of the core request.

  const HoverModeStatus({required this.isHoverMode});
}

/// 从原生传递过来的参数，折叠屏状态
/// JSON传递 {"state":1,"orientation":0,"bounds":{"left":0,"right":1840,"top":1104,"bottom":1104}}
/// state（折叠状态）：0：未知，1：展开，2：折叠，3：半折叠
/// orientation（折痕方向）：0：水平，1：垂直
/// bounds（折叠区域）：包含四个属性，left、right、top、bottom，分别表示折叠区域的左、右、上、下边界，单位为像素.
class FoldingFeature {
  ///折叠状态
  final FoldStatus state;

  ///折痕方向
  final Axis orientation;

  ///折叠区域
  final Rect bounds;

  FoldingFeature(
      {this.state = FoldStatus.unknown,
      this.orientation = Axis.horizontal,
      this.bounds = Rect.zero});

  factory FoldingFeature.fromJson(Map<String, dynamic> json) {
    FoldStatus stateFromInt(int state) {
      switch (state) {
        case 1:
          return FoldStatus.expanded;
        case 2:
          return FoldStatus.folded;
        case 3:
          return FoldStatus.halfFolded;
        default:
          return FoldStatus.unknown;
      }
    }

    Axis axisFromInt(int orientation) {
      switch (orientation) {
        case 1:
          return Axis.vertical;
        default:
          return Axis.horizontal;
      }
    }

    return FoldingFeature(
      state: stateFromInt(json['state']),
      orientation: axisFromInt(json['orientation']),
      bounds: Rect.fromLTRB(
          (json['bounds']["left"]).toDouble(),
          (json['bounds']["top"]).toDouble(),
          (json['bounds']["right"]).toDouble(),
          (json['bounds']["bottom"]).toDouble()),
    );
  }
}

///折叠状态枚举类
enum FoldStatus {
  ///表示设备当前折叠状态未知
  unknown,

  ///表示设备当前折叠状态为完全展开
  expanded,

  ///表示设备当前折叠状态为折叠
  folded,

  ///表示设备当前折叠状态为半折叠
  ///半折叠指完全展开和折叠之间的状态
  halfFolded,
}
